package gatecontrol.network.http;

import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import gatecontrol.network.beans.GateUpLoad;
import gatecontrol.network.beans.SluiceControl;
import com.xkh.xkh.MyApplication;
import managers.VolleySingleton;

/**
 * 发送闸控命令
 * Created by nimi on 2015/11/14.
 */
public class SendGateControl {
    Delegate delegate;
    UploadSluiceControlData upload;

    public SendGateControl(Delegate delegate, UploadSluiceControlData upload) {
        this.delegate = delegate;
        this.upload = upload;
    }

    public void send(final GateUpLoad gateUpLoad) {//新的请求执行上传adcp
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP +"/guanqu/GetSluiceControl";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response, SendGateControl.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.print(error);
                if (delegate != null)
                    delegate.onError(error, SendGateControl.this);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {//数据填充
                Map map = new HashMap();
                map.put("gateNum", gateUpLoad.getGate_num());
                map.put("stadiacode", gateUpLoad.getStadia_code());
                map.put("person", gateUpLoad.getSend_person());
                map.put("time", gateUpLoad.getSend_time() + "");
                map.put("command", gateUpLoad.getCommand() + "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    // 发送
    public void send() {//新的请求执行上传adcp
//        String url ="http://192.168.23.1:8080/imServer/guanqu/GetSluiceControl";
        String ip = MyApplication.share().getString(R.string.IP);
        String url = "http://" + ip + "/guanqu/GetSluiceControl";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MyApplication.share(), "发送成功", Toast.LENGTH_SHORT).show();
                        if (delegate != null)
                            delegate.onResponse(response, SendGateControl.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.print(error);
                Toast.makeText(MyApplication.share(), "网络异常", Toast.LENGTH_SHORT).show();
                if (delegate != null)
                    delegate.onError(error, SendGateControl.this);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {//数据填充
                Map map = new HashMap();
                map.put("data", JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public static interface Delegate {
        public void onResponse(String s, SendGateControl send);

        public void onError(VolleyError volleyError, SendGateControl send);
    }

    // 上行数据
    public static class UploadSluiceControlData {
        SluiceControl sc = new SluiceControl();

        public SluiceControl getSc() {
            return sc;
        }

        public void setSc(SluiceControl sc) {
            this.sc = sc;
        }
    }
}


